package com.kt.uokos.yimo.controller;


import com.kt.uokos.yimo.anno.validator.Order;
import com.kt.uokos.yimo.anno.validator.Sort;
import com.kt.uokos.yimo.dao.Keyword;
import com.kt.uokos.yimo.service.KeyWordService;
import com.kt.uokos.yimo.util.ResponseUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin/keyword")
public class KeywordController {

    @Resource
    private KeyWordService keyWordService;


    @GetMapping("list")
    public Object list(String keyword, String url,
                       @RequestParam(defaultValue = "1")Integer page,
                       @RequestParam(defaultValue = "10")Integer limit,
                       @Sort@RequestParam(defaultValue = "add_time")String sort,
                       @Order@RequestParam(defaultValue = "desc")String order){

        List<Keyword> list = keyWordService.querySelective(keyword, url, page, limit, sort, order);
        return ResponseUtil.okList(list);
    }

    @PostMapping("create")
    public Object create(@RequestBody Keyword keyword){

        if (StringUtils.isEmpty(keyword)) {
            return ResponseUtil.errs();
        }
        keyWordService.create(keyword);
        return ResponseUtil.ok(keyword);
    }

    @PostMapping("delete")
    public Object delete(@RequestBody Keyword keyword){
        if (keyword.getId()==null){
            return ResponseUtil.errs();
        }
        keyWordService.delete(keyword.getId());
        return ResponseUtil.ok();
    }

    @PostMapping("update")
    public Object update(@RequestBody Keyword keyword){
        if (StringUtils.isEmpty(keyword)) {
            return ResponseUtil.errs();
        }
        //对比其是否改动
        if (keyWordService.update(keyword)==0){
            return ResponseUtil.errs();
        }
        return ResponseUtil.ok(keyword);
    }



}
